import java.util.PriorityQueue;

class KthLargest703 {
    private PriorityQueue<Integer> queue = new PriorityQueue<>() ;
    private int max = 0 ;
    public KthLargest703(int k, int[] nums) {
        this.max = k ;
        for(int i : nums){
            if(queue.size() < k){
                queue.offer(i) ;
            }else{
                if(i > queue.peek()){
                    queue.poll() ;
                    queue.offer(i) ;
                }
            }
        }
    }

    public int add(int val) {
        if(queue.size() < max){
            queue.offer(val);
            return queue.peek() ;
        }else{
            if(val > queue.peek()){
                queue.poll() ;
                queue.offer(val);
            }
        }
        return queue.peek() ;
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */